########################################################################
# $HeadURL: http://dirac-grid.googlecode.com/svn/BelleDIRAC/tags/BelleDIRAC/v3r0-pre2/DataManagementSystem/DB/IntegratedFCComponents/Utilities.py $
########################################################################

""" DIRAC FileCatalog utilities
"""

__RCSID__ = "$Id: Utilities.py 224 2010-07-29 09:44:04Z ricardo.graciani $"

import md5, random, os
from types import *
from DIRAC import S_OK, S_ERROR

def checkArgumentFormat( path ):
  """ Bring the various possible form of arguments to FileCatalog methods to
      the standard dictionary form
  """
  result = checkArgumentDict( path )
  if not result['OK']:
    return result

  # Bring the lfn path to the normalized form
  urls = {}
  for url in path:
    normpath = os.path.normpath( url )
    urls[normpath] = path[url]
  return S_OK( urls )

def checkArgumentList( path ):
  """ Check and process format of the arguments to FileCatalog methods """
  if type( path ) in StringTypes:
    urls = [path]
  elif type( path ) == ListType:
    urls = path
  elif type( path ) == DictType:
    urls = path.keys()
  else:
    return S_ERROR( "checkArgumentList: Supplied path is not of the correct format" )
  return S_OK( urls )

def checkArgumentDict( path ):
  """ Check and process format of the arguments to FileCatalog methods """
  if type( path ) in StringTypes:
    urls = {path:False}
  elif type( path ) == ListType:
    urls = {}
    for url in path:
      urls[url] = False
  elif type( path ) == DictType:
    urls = path
  else:
    return S_ERROR( "checkArgumentDict: Supplied path is not of the correct format" )
  return S_OK( urls )
